package com.iuie.ns.frame.utils;

import java.util.ArrayList;
import java.util.List;

import com.iuie.ns.frame.config.AppConfigUtil;
import com.iuie.ns.web.html.HTMLTag;

/**
 * markdown文档工具类
 * @author liujie
 *
 */
public class MarkdownUtils {
	
	public static final String CSS_PREFIX = AppConfigUtil.getInstance().getProperty("markdown.css.prefix");

//	public static final String CSS_PREFIX = "http://www.iuie.club:8080/cdn/css/";
	
	public static final String JS_PREFIX = AppConfigUtil.getInstance().getProperty("markdown.js.prefix");
	
//	public static final String JS_PREFIX = "http://www.iuie.club:8080/cdn/js/";
	
	
	private MarkdownUtils() {}
	
	
	public static final List<String> STATIC_RES = new ArrayList<String>(){
		private static final long serialVersionUID = -5860288822734310329L;

		{
			add("editormd.min.css");
			add("jquery-1.9.1.min.js");
			add("marked.min.js");
			add("prettify.min.js");
			add("raphael.min.js");
			add("underscore.min.js");
			add("sequence-diagram.min.js");
			add("flowchart.min.js");
			add("jquery.flowchart.min.js");
			add("editormd.min.js");
		}
	};
	
	public static final HTMLTag $HTML = new HTMLTag("html");
	public static final HTMLTag $TITLE = new HTMLTag("title");
	public static final HTMLTag $TEXTAREA;
	
	static {
		
		HTMLTag $head = new HTMLTag("head");
		
		HTMLTag $meta = new HTMLTag("meta");
		$meta.addAttribute("http-equiv", "Content-Type")
			.addAttribute("content", "text/html; charset=utf-8");
		
		$head.addContentNode($TITLE)
			.addContentNode($meta);
			
		for (String res : STATIC_RES) {
			if (StringUtils.endsWith(res, ".css")) {
				HTMLTag $link = new HTMLTag("link");
				$link.addAttribute("rel", "stylesheet")
					.addAttribute("href", CSS_PREFIX + res);
				$head.addContentNode($link);
			}
			if (StringUtils.endsWith(res, ".js")) {
				HTMLTag $script = new HTMLTag("script");
				$script.addAttribute("type", "text/javascript")
					.addAttribute("src", JS_PREFIX + res);
				$head.addContentNode($script);
			}
			
		}
		
		$TEXTAREA = new HTMLTag("textarea");
		$TEXTAREA.addStyle("style", "display:none");
		
		HTMLTag $div = new HTMLTag("div");
		$div.addAttribute("id", "test-editormd")
			.addContentNode($TEXTAREA);
		
		HTMLTag $body = new HTMLTag("body");
		$body.addStyle("font-family", "Helvetica, arial, freesans, clean, sans-serif")
			.addStyle("font-size", "14px")
			.addStyle("line-height", "1.6")
			.addStyle("color", "#333")
			.addStyle("background-color", "#fff")
			.addStyle("padding", "20px")
			.addStyle("max-width", "960px")
			.addStyle("margin", "0 auto");
		
		HTMLTag $script = new HTMLTag("script");
		$script.addAttribute("type", "text/javascript");
		
		String srciptText = "editormd.markdownToHTML('test-editormd', {"
				+ "'htmlDecode':'style,script,iframe',"
				+ "'emoji':true,"
				+ "'taskList':true,"
				+ "'tex':true,"
				+ "'flowChart':true,"
				+ "'sequenceDiagram':true});";
		$script.setTextNode(srciptText);
		
		$body.addContentNode($div)
			.addContentNode($script);
		
		$HTML.addContentNode($head)
			.addContentNode($body);
		
	}
	
	public static String toHtml(String title, String text) {
		$TITLE.setTextNode(title);
		$TEXTAREA.setTextNode(text);
		return "<!DOCTYPE html>" + $HTML.toString();
	}
	
}
